import request from '@/utils/request'
import type { PageDto, PageReqDto, PagesDto } from '../types'
import type { BatchInvoiceDto, InvoiceDto, InvoiceReqDto, SyncInvoiceReqDto } from './types'


// 获取发票列表
export function getInvoiceList(params: PageReqDto & Partial<InvoiceReqDto>): Promise<PageDto<InvoiceDto>> {
    return request.get('/crm-prod/saas/invoice/list', {
        params
    })
}


/**
 * 上传发票zip包
 * @param data
 * @param data.file file
 * @param id 固定传 0
 */
export function uploadInvoiceZip(data: FormData, id: number = 0): Promise<string> {
    return request.post(`/crm-prod/saas/invoice/uploadZip-old?id=${id}`, data)
}

/**
 * 批量上传发票zip包
 * @param data
 * @param data.file file
 * @param data.invoiceFrom 发票来源
 */
export function uploadBatchInvoiceZip(data: FormData ): Promise<string> {
    return request.post('/crm-prod/saas/invoice/upload-zip-new', data)
}

/**
 *  上传发票图片
 * @param data 
 * @param id 
 * @returns 
 */
export function uploadInvoice(data: FormData, id: number): Promise<string> {
    return request.post(`/crm-prod/saas/invoice/upload?id=${id}`, data)
}

/**
 * 导出发票
 * @param params
 * @returns
 */
export function exportInvoice(params: Partial<InvoiceReqDto>): Promise<string> {
    return request.get('/crm-prod/saas/invoice/export', {
        params
    })
}

/**
 * 批量上传的发票列表
 * @param params 
 * @returns 
 */
export function getInvoiceBatchList(params: PageReqDto & Partial<InvoiceReqDto>): Promise<PageDto<BatchInvoiceDto>> {
    return request.get('/crm-prod/saas/invoice/upload/record/record-list', {
        params
    })
}

/**
 * // 获取需要同步（有冲突）的发票列表
 * @param params 
 * @param params.isSync 0 未同步 1 已同步
 * @param params.month string 月份
 * @returns 
 */
export function getNeedSyncInvoiceList(params: {
    isSync: 0 | 1
    month?: string
}): Promise<BatchInvoiceDto[]> {
    return request.get('/crm-prod/saas/invoice/upload/record/need-sync-invoice-list', {
        params
    })
}

/**
 * 根据上传发票查询需同步应用列表
 * @param params 
 * @returns 
 */
export function getNeedUploadList(params: {
    companyName: string
    voiceType: number
}): Promise<InvoiceDto[]> {
    return request.get('/crm-prod/saas/invoice/query-need-upload-list', {
        params
    })
}

/**
 * 处理同步发票
 * @param data 
 * @returns 
 */
export function syncInvoice(data: SyncInvoiceReqDto[]): Promise<InvoiceDto[]> {
    return request.post('/crm-prod/saas/invoice/sync-invoice', data)
}

/**
 * 撤销发票异常状态
 * @param data 
 * @returns 
 */
export function cancelInvoiceUplaod(data: {
    ids: number[]
}): Promise<void> {
    return request.put('/crm-prod/saas/invoice/cancel-invoice-upload', data)
}

/**
 * 导入开票公司
 * @param data 
 * @returns 
 */
export function uploadOpenCompany(data: FormData): Promise<void> {
    return request.post('/crm-prod/saas/invoice/import-open-company', data)
}
